<template>
  <div class="common-layout h100">
    <el-container class="h100">
      <el-header class="header-container">
        <!-- 左侧logo和标题区域 -->
        <div class="header">
          <div class="logo-container">
            <div class="logo"></div>
          </div>
          <span class="title">齐都体育城预约管理系统</span>
        </div>
        <!-- 右侧用户信息 -->
        <div class="user-info">
          <el-dropdown>
            <span class="el-dropdown-link">
              <el-avatar v-if="baseUrl + AdminModel.avatar" :size="36"
                         :src="baseUrl + AdminModel.avatar"/>
              <el-icon v-else><Avatar/></el-icon>
              <span class="username">管理员</span>
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="profile">个人中心</el-dropdown-item>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-container>
        <el-aside :class="['aside-container', isCollapse ? 'collapsed' : '']">
          <!-- 导航菜单 -->
          <div class="menu-toggle" @click="toggleMenu">
            <el-icon><expand v-if="isCollapse" /><fold v-else /></el-icon>
          </div>
          <el-menu
              class="nav"
              router
              text-color="#fff"
              active-text-color="#ffd04b"
              background-color="#545c64"
              default-active="/dashboard"
              :collapse="isCollapse"
              :collapse-transition="false">
            <template v-for="mi in menuItems" :key="mi.name">
              <el-sub-menu v-if="Array.isArray(mi.children)" :index="mi.name">
                <template #title>
                  <el-icon><location /></el-icon>
                  <span>{{ mi.name }}</span>
                </template>
                <el-menu-item v-for="smi in mi.children" :key="smi.url" :index="smi.url">
                  <template #title>
                    <span>{{ smi.name }}</span>
                  </template>
                </el-menu-item>
              </el-sub-menu>
              <el-menu-item v-else :index="mi.url">
                <el-icon><icon-menu /></el-icon>
                <template #title>
                  <span>{{ mi.name }}</span>
                </template>
              </el-menu-item>
            </template>
          </el-menu>
        </el-aside>
        <el-main class="main-content">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>

  <!-- 个人中心对话框 -->
  <el-dialog title="个人中心" v-model="showProfileDialog" width="500px" align-center>
    <el-form label-width="80px" :model="AdminModel">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="AdminModel.username" placeholder="请输入用户名" disabled></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <el-upload
            class="avatar-uploader"
            action="/api/users/admin/upload/avatar"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="headers">
          <el-avatar v-if="AdminModel.avatar"
                     :src="baseUrl + AdminModel.avatar" size="large"></el-avatar>
          <el-icon v-else class="avatar-uploader-icon">
            <Plus/>
          </el-icon>
        </el-upload>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="showProfileDialog = false">取消</el-button>
      <el-button type="primary" @click="updateProfile">提交</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>
.h100 {
  height: 100%;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* 头部容器优化 */
.header-container {
  height: 80px;
  background: linear-gradient(120deg, #4A90E2, #50C8C6);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 0 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 头部内容区 */
.header {
  display: flex;
  align-items: center;
  height: 100%;
}

/* Logo美化 */
.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 60px;
  width: 60px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  backdrop-filter: blur(10px);
  margin-right: 20px;
}

.logo {
  height: 40px;
  width: 40px;
  background: url("@/assets/logo.png") no-repeat center center/cover;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.logo:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(255, 255, 255, 0.2);
}

/* 标题美化 */
.title {
  font-size: 22px;
  font-weight: 600;
  color: #ffffff;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 用户信息区域 */
.user-info {
  display: flex;
  align-items: center;
  color: white;
}

.username {
  margin: 0 10px;
  font-weight: 500;
}

/* 侧边栏美化 */
.aside-container {
  width: 200px;
  /* 美化后的侧边栏渐变色 */
  background: linear-gradient(160deg, #2c3e50, #34495e, #2c3e50);
  transition: all 0.3s ease;
  position: relative;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

.aside-container.collapsed {
  width: 64px;
}

/* 菜单切换按钮 */
.menu-toggle {
  position: absolute;
  right: 10px;
  top: 10px;
  z-index: 1000;
  color: white;
  cursor: pointer;
  padding: 5px;
  border-radius: 4px;
  transition: all 0.3s;
  background: rgba(255, 255, 255, 0.1);
}

.menu-toggle:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

/* 菜单美化 */
.nav {
  border-right: none;
  height: calc(100% - 50px);
  margin-top: 50px;
  padding-top: 15px;
  background: transparent;
}

/* 菜单项优化 */
:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  height: 50px;
  line-height: 50px;
  margin: 0 8px;
  border-radius: 6px;
  font-size: 14px;
  transition: all 0.2s ease;
}

:deep(.el-menu-item:hover),
:deep(.el-sub-menu__title:hover) {
  background-color: rgba(255, 255, 255, 0.15) !important;
  transform: translateX(3px);
}

:deep(.el-menu-item.is-active) {
  background-color: #4A90E2 !important;
  color: #fff !important;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

:deep(.el-sub-menu .el-menu-item) {
  padding-left: 40px !important;
}

:deep(.el-menu--collapse .el-menu-item .el-icon) {
  margin-right: 0 !important;
}

:deep(.el-menu--collapse .el-sub-menu .el-sub-menu__title span) {
  height: 0;
  width: 0;
  overflow: hidden;
  visibility: hidden;
  display: inline-block;
}

:deep(.el-menu--collapse .el-menu-item .el-tooltip) {
  padding-left: 20px !important;
}

/* 主内容区美化 */
.main-content {
  padding: 20px;
  background: url("@/assets/main_bg.png") no-repeat center center/cover;
  overflow-y: auto;
  min-height: calc(100vh - 80px);
}

/* 滚动条美化 */
:deep(::-webkit-scrollbar) {
  width: 8px;
  height: 8px;
}

:deep(::-webkit-scrollbar-track) {
  background: #f1f1f1;
  border-radius: 4px;
}

:deep(::-webkit-scrollbar-thumb) {
  background: linear-gradient(180deg, #4A90E2, #50C8C6);
  border-radius: 4px;
}

:deep(::-webkit-scrollbar-thumb:hover) {
  background: #4A90E2;
}

/* 响应式优化 */
@media (max-width: 768px) {
  .aside-container {
    width: 64px;
  }

  .title {
    font-size: 18px;
  }

  .header-container {
    padding: 0 20px;
  }
}
.avatar-uploader {
  width: 120px; /* 正方形宽度 */
  height: 120px; /* 正方形高度（与宽度一致） */
  border: 1px dashed #d9d9d9;
  border-radius: 4px; /* 可选：轻微圆角 */
  cursor: pointer;
  display: flex; /* 启用flex布局 */
  align-items: center; /* 垂直居中 */
  justify-content: center; /* 水平居中 */
  overflow: hidden; /* 防止图片溢出容器 */
}

.avatar {
  width: 100%;
  height: 100%;
  background-size: cover; /* 保持比例，覆盖整个容器 */
  background-position: center; /* 图片居中显示 */
  background-repeat: no-repeat; /* 不重复显示 */
}

/* 上传图标居中显示，调整大小 */
.avatar-uploader-icon {
  font-size: 24px; /* 图标大小适中 */
  color: #8c8c8c; /* 图标颜色（灰色，符合默认风格） */
}
</style>

<script setup>
// 脚本部分保持不变
import {onMounted, reactive, ref} from "vue";
import {ArrowDown, Avatar, Edit, Expand, Fold, Location, Menu as IconMenu, Plus, User} from '@element-plus/icons-vue';
import {decodeJwt, getJwt, removeJwt} from "@/api/jwt.js";
import api from "@/util/api.js";
import {baseUrl} from "@/api/base.js";
import router from "@/router/index.js";
import {ElMessage} from "element-plus";

//头像请求头
const headers = {
  Authorization: getJwt()
};

//头像上传成功
function handleAvatarSuccess(resp) {
  AdminModel.value.avatar = resp.data;
  console.log("当前头像 URL:", AdminModel.value.avatar); // 确认是否为正确的可访问 URL
}

//管理员表单模型
let AdminModel = ref({
  username: "",
  password: "",
  confirmPassword: "",
  avatar: ""
})

//表单状态
let showProfileDialog = ref(false);

//获取管理员信息
async function getAdminInfo() {
  //console.log(decodeJwt());
  let resp = await api({
    url: "/users/admin/info",
    method: "get",
    params: {
      username: decodeJwt().username
    }
  });
  //console.log("获取管理员信息成功:", resp.data);
  AdminModel.value = resp.data;
  //console.log("获取管理员信息成功:", resp.data)
}

//个人中心
function profile() {
  /*const response = await getAdminInfo();
  AdminModel.value = response.data;*/
  console.log(AdminModel.value)
  showProfileDialog.value = true;
}

//退出登录
function logout() {
  removeJwt();
  router.push("/login");
  //刷新页面
  window.location.reload();
}

//控制菜单折叠状态
const isCollapse = ref(false);

//切换菜单折叠状态
const toggleMenu = () => {
  isCollapse.value = !isCollapse.value;
};

//所有导航菜单
const menuItems = reactive([
  {
    name: "数据看板",
    url: "/admin/main/dashboard",
    icon: "Odometer"
  }, {
    name: "客户管理",
    icon: "User",
    children: [
      {
        name: "客户列表",
        url: "/admin/main/customer"
      }
    ]
  }, {
    name: "场地管理",
    icon: "Location",
    children: [
      {
        name: "场地列表",
        url: "/admin/main/venue"
      }
    ]
  }, {
    name: "预约管理",
    icon: "Setting",
    children: [
      {
        name: "预约列表",
        url: "/admin/main/reservation"
      },
      {
        name: "历史记录",
        url: "/admin/main/history"
      }
    ]
  }
]);

//提交修改
function updateProfile() {
  console.log(AdminModel.value.username);
  let resp = api({
    url: "/users/admin",
    method: "put",
    data: AdminModel.value
  })
  if (resp.success) {
    ElMessage.success("修改成功");
    showProfileDialog.value = false;
  } else {
    ElMessage.error("修改失败");
  }
}

//加载
onMounted(() => {
  getAdminInfo();
})
</script>